//
//  SEGHTTPSessionManager.h
//  SEGBasic
//
//  Created by 刘山 on 2018/3/14.
//  Copyright © 2018年 刘山. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

#pragma mark - 图片上传相关
UIKIT_EXTERN NSString * const SEGMimeTypeJPEG;//图片
UIKIT_EXTERN NSString * const SEGMimeTypeAudio;//语音
UIKIT_EXTERN NSString * const kSEGUsualCookie;//Cookies

@interface SEGUploadFileModel : NSObject
@property (nonatomic, copy) NSString *fileName;//这个文件在服务器的名字。需要携带格式:aaa.jpg，
@property (nonatomic, copy) NSString *name;//上传到服务器，接受这个图片的字段名,默认：file,这个值可以为空。
@property (nonatomic, strong)NSData *data;
@property (nonatomic, copy) NSString *mimeType;//上传的文件的类型。不可为空
@end

#pragma mark - 枚举

//网络状态
typedef NS_ENUM(NSInteger, SEGNetworkStatus) {
    SEGNetworkStatusUnknown = -1, //未知
    SEGNetworkStatusNotReachable = 0,// 无连接
    SEGNetworkStatusViaWWAN = 1, // 蜂窝网络
    SEGNetworkStatusViaWiFi = 2,// 局域网络
};

//网络请求方式
typedef NS_ENUM(NSInteger, SEGHTTPSessionManagerRequestMethod) {
    GET, //GET请求方式
    POST, //POST请求方式
    DELETE //DELETE请求方式
};

//请求返回类型-旧版本
typedef NS_ENUM(NSInteger, SEGHTTPSessionManagerResponseType) {
    PROGRESS,   //类型是进度条
    RESULT  //类型是返回值
};

#pragma mark - block类型定义
typedef void (^SEGNetworkRequestCallBack)(id result, NSError* error);
typedef void (^SEGNetworkStatusCallBack)(SEGNetworkStatus status);
typedef void (^SEGNetworkRequestCallBackProgress)(SEGHTTPSessionManagerResponseType responseType ,double progressValue, id result, NSError* error);

@interface SEGHTTPManager : NSObject

@property (nonatomic, assign) NSTimeInterval timeoutInterval;//设置请求超长时间
@property (atomic, strong)NSDictionary *headerFields;//设置请求头参数
/**
 * 全局单利
 * @author Sam
 */
+ (instancetype)sharedNetworkManager;

/**
 * 获取当前网络状态
 * @author Sam
 * @param block  状态回调
 */
-(void)segNetworkStatus:(SEGNetworkStatusCallBack)block;

/**
 获取当前网络状态
 
 @return 状态回调
 */
- (SEGNetworkStatus)segNetworkStatus;

/**
 * 网络请求
 * @author Sam
 * @param method  请求方式
 * @param URLString  请求地址
 * @param parameters  请求参数
 * @param finished  请求结果回调
 */
- (void)requestWithMethod:(SEGHTTPSessionManagerRequestMethod)method URLString:(NSString*)URLString parameters:(id)parameters finished:(SEGNetworkRequestCallBack)finished;

/**
 * JSON格式网络请求
 * @author Sam
 * @param method  请求方式
 * @param URLString  请求地址
 * @param parameters  请求参数
 * @param finished  请求结果回调
 */
- (void)requestJSONWithMethod:(SEGHTTPSessionManagerRequestMethod)method URLString:(NSString*)URLString parameters:(id)parameters finished:(SEGNetworkRequestCallBack)finished;

/**
 * 上传录音
 * @author Sam
 * @param URLString  图片上传地址
 * @param parameters  其他参数
 * @param recordData  录音数据源
 */
-(void)uploadRecordWithURLString:(NSString *)URLString
                      parameters:(id)parameters
                      recordData:(NSData*)recordData
                        finished:(SEGNetworkRequestCallBackProgress)finished;

/**
 * 上传多张图片
 * @author Sam
 * @param URLString  图片上传地址
 * @param parameters  其他参数
 * @param imageDataArray  图片集合
 */
-(void)uploadImagesWithURLString:(NSString *)URLString
                      parameters:(id)parameters
                   WithDataArray:(NSArray<NSData*>*)imageDataArray
                        finished:(SEGNetworkRequestCallBackProgress)finished;

/**
 * 取消所有的网络请求
 * @author Sam
 */
-(void)segCancelAllRequest;

/**
 * 移除所有cookies缓存
 * @author Sam
 */
- (void)removeCookies;

/// 手动保存cookie
/// @param url cookie所属url
/// @param response 响应体
+ (void)seg_saveCookiesWithURL:(NSURL *)url httpResponse:(NSURLResponse *)response;

/// 获取本地cookie
/// @param url cookie所属url
+ (NSString *)seg_getCookiesDictionaryWithURL:(NSURL *)url;

#pragma mark - 上传文件
/**
 上传文件到服务器
 
 @author Sam
 @param urlString 服务地址
 @param parameters 上传参数
 @param sources 数据源
 @param progress 上传进度
 @param completed 完成回调
 */
-(void)uploadDatasWithURLString:(NSString*)urlString
                     parameters:(id)parameters
                        sources:(NSArray<SEGUploadFileModel*>*)sources
                       progress:(void(^)(double progressValue))progress
                      completed:(void (^) (id result,NSError * error))completed;

#pragma mark - 上传声音文件
-(void)uploadVoiceWithURLString:(NSString*)urlString
                     parameters:(id)parameters
                         source:(NSData*)source
                       fileName:(NSString*)fileName
                       progress:(void(^)(double progressValue))progress
                      completed:(void (^) (id result,NSError * error))completed;

-(void)uploadVoiceWithURLString:(NSString*)urlString
                     parameters:(id)parameters
                         source:(NSData*)source
                       progress:(void(^)(double progressValue))progress
                      completed:(void (^) (id result,NSError * error))completed;

-(void)uploadVoicesWithURLString:(NSString*)urlString
                      parameters:(id)parameters
                         sources:(NSArray<NSData*>*)sources
                        progress:(void(^)(double progressValue))progress
                       completed:(void (^) (id result,NSError * error))completed;

#pragma mark - 上传图片文件
-(void)uploadImagesWithURLString:(NSString*)urlString
                      parameters:(id)parameters
                         sources:(NSArray<NSData*>*)sources
                        progress:(void(^)(double progressValue))progress
                       completed:(void (^) (id result,NSError * error))completed;

-(void)uploadImageWithURLString:(NSString*)urlString
                     parameters:(id)parameters
                         source:(NSData*)source
                       fileName:(NSString*)fileName
                       progress:(void(^)(double progressValue))progress
                      completed:(void (^) (id result,NSError * error))completed;

-(void)uploadImageWithURLString:(NSString*)urlString
                     parameters:(id)parameters
                         source:(NSData*)source
                       progress:(void(^)(double progressValue))progress
                      completed:(void (^) (id result,NSError * error))completed;
@end

